
package model;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;


import main.GameCanvas;
import screen.PaintPopup;
import utilities.CRes;
import utilities.GameResource;
import utilities.TileMap;
import font.BitmapFont;

public class Animal extends Item{
	public String m_strChatAnimal;
    public boolean m_bEnableChat = false;
    private int m_iTimeShowChat = 0;
    
	//direction
	public static int[] m_delta = {-1, 0, 1};
	
	//animal action
	public static final int STAND = 0;
	public static final int RUN = 1;
	public static final int SITTING = 2;
	public static final int SLEEP = 3;
	public static final int EAT = 4;
	public static final int TAKEABATH = 5;
		
	//animal properties
//	public float m_iSpeedX;	//toc do theo x
//	public float m_iSpeedY; 	//toc do theo y
	public int m_iAction;	//hanh dong cua vat nuoi
	public Position m_position = new Position();//vi tri
	//public byte m_iDirection;//huong di chuyen cua con vat
	public int m_iLifeTime;//thoi gian no da sinh song
	public AnimalInfo animalInfo;
	public int m_iLost;//so tien bi an trom
	public int m_iHealth;
	public int m_iSick;
	public int m_iSmart;
	public int m_iGender;
	
	public int m_iFeedFood;
	public int m_iBonusHealth;
	public int m_iBonusTime;//thoi gian duoc song them (don vi ngay)
	
	//////	
	public boolean m_bShowStatus = true;
	
	public boolean m_bIsDie;
	public boolean m_bIsHungry;
	public boolean m_bIsDirty;
	public boolean m_bIsHaveMoney;
	//....
		
	//dog action run
	private int m_iActionRunDog = 0;
	private int m_iChangeActionRunDog = 0;
	//khu vuc di chuyen cua vat nuoi
	public int m_iLeft;
	public int m_iTop;
	public int m_iBoundW;
	public int m_iBoundH;
	//....

	//Time variables
	private int m_iGameTick = CRes.random(100, 200);;
	private int m_iTimeChangeDirection = CRes.random(50, 100);
	private int m_iEyesDog = 0; // 0 la mo
	public int m_iTimeEndChatAnimal = 200;
	
	public int m_iGoToAction = 0;// duy chuyển con vật dến 1 noi - 1: ăn - 2 - ttam 
	public boolean m_bCompleteAction = false;
	public boolean m_bActionBath = false;
	//// init coin
	private int m_iTimeMoney = 0;
	private int iconIndex = 0;
	private int m_iDelta = 0;
	
	
	public int m_iAnimalStatus;
	public void setTimeLife(int timeLife){
		m_iLifeTime = timeLife;
		if(animalInfo != null ){
			int tmp = animalInfo.m_sTimeLiving * 480;
			if(tmp >= m_iLifeTime){
				m_iAnimalStatus = 0;
			}
			else {
				m_iAnimalStatus = 1;
			}
		}
		else{
			m_iAnimalStatus = 1;
		}
	}
	
	public int getTimeLife()
	{
		return m_iLifeTime;
	}
	
	public void copyTo(Animal ani){
		ani.m_iId = this.m_iId;
		ani.m_strName = this.m_strName;
		ani.m_iHealth = this.m_iHealth;
		ani.m_iSmart = this.m_iSmart;
		ani.m_iGender = this.m_iGender;
		ani.setTimeLife(this.getTimeLife());
		ani.m_iSick = this.m_iSick;
		ani.m_bIsDie = this.m_bIsDie;
		ani.m_bIsHungry = this.m_bIsHungry;
		ani.m_bIsDirty = this.m_bIsDirty;
		ani.m_iBonusHealth = this.m_iBonusHealth;
		ani.m_iBonusTime = this.m_iBonusTime;
	}
	
	public MyObj clone() {
		// TODO Auto-generated method stub
		return null;
	} 
		
	public Animal(int type) {
		// TODO Auto-generated constructor stub
		animalInfo = DataManager.gI().getAnimalInfo(type);		
	}
	
	public void start(Position pos){
		m_position = pos;
		m_position.anchor = ((CRes.random(0,2) == 0)? 0 : 1);
		m_iAction = CRes.random(0, 4);	
		isSick();
	}
	private float m_tmpx, m_tmpy, m_tmpxinc, m_tmpyinc, m_tmpdx, m_tmpdy;
	private int m_tmpstep;
	public boolean m_tmpIsinitPosition = false;
	private void initPosition(int x2, int y2){	
			
		m_tmpdx = x2-m_position.x;
		m_tmpdy = y2-m_position.y;
		if( Math.abs(m_tmpdx)> Math.abs(m_tmpdy))
			m_tmpstep = (int) Math.abs(m_tmpdx);
		else
			m_tmpstep = (int) Math.abs(m_tmpdy);
		
		m_tmpxinc=m_tmpdx/m_tmpstep;
		m_tmpyinc=m_tmpdy/m_tmpstep;
		
		float speed = 2;// mặc dịnh cho con chó ở ngoài login
		if(animalInfo != null){
			speed = animalInfo.m_fSpeed[m_iAnimalStatus];
			if(m_iAction == EAT || m_iAction == TAKEABATH){
				speed *= 2;
			}
		}
		
		m_tmpxinc *= speed;
		m_tmpyinc *= speed;
		m_tmpstep /= speed;

		m_tmpx=m_position.x;
		m_tmpy=m_position.y;	
	}
	
	public void Direction(int tmpDirection){		
		if(animalInfo != null ){
			int tmpX = animalInfo.m_iW[m_iAnimalStatus];
			
			if(tmpDirection != m_position.anchor){
				if(tmpDirection == Direction.LEFT)
					m_position.x -= tmpX;
				else
					m_position.x += tmpX;				
			}
		}
		m_position.anchor = (byte) tmpDirection;
	}
	
	public void isSick(){
		//animal sick
		/*
		if(animalInfo != null){
			if(m_iSick != -1 && m_iSick != 0 ){
				m_iAction = CRes.random(0, 4);
				if(m_iAction == RUN)
					m_iAction = SLEEP;
			}		
		}
		*/
	}
	
	private void getToPosition(){
		if(!m_tmpIsinitPosition){
			int x2 = 0,y2 = 0;
			switch (m_iAction) {
			case RUN:
				x2 = CRes.random(m_iLeft, m_iLeft+m_iBoundW);
				y2 = CRes.random(m_iTop, m_iTop+m_iBoundH);
				break;
			case EAT:
				Direction(Direction.RIGHT);
				
				x2 = 5 + GameResource.instance.imgFeeder.getWidth() + animalInfo.m_bodyPart[m_iAnimalStatus].m_iW - (animalInfo.m_headPart[m_iAnimalStatus].m_iW/3);
				int a = GameCanvas.h - GameCanvas.hBottomBar - GameResource.instance.imgFeeder.getHeight() - 50 + animalInfo.m_iH[m_iAnimalStatus];
				int nextA = a + GameResource.instance.imgFeeder.getHeight() - animalInfo.m_iH[m_iAnimalStatus];
				if(m_position.x > x2+5 || m_position.y>nextA || m_position.y < a){
					y2 = CRes.random( a, nextA);
				}
				else{
					y2 = m_position.y;
				}
				break;
			case TAKEABATH:
				Direction(Direction.LEFT);
				
				int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
		        tmpY = GameCanvas.h - tmpY*2/3 + 10;
				x2 = GameCanvas.w - 50;
				y2 = tmpY+40;
				
				break;
			default:
				break;
			}
			//change direction
			if(m_position.x < x2){
				Direction(Direction.LEFT);
			}
			if(m_position.x > x2)
				Direction(Direction.RIGHT);
			
			initPosition(x2, y2);	
			m_tmpIsinitPosition = true;
			
		}
		else{
			m_position.x = (int) m_tmpx;
			m_position.y = (int) m_tmpy;	
			m_tmpx=m_tmpx+m_tmpxinc;
			m_tmpy=m_tmpy+m_tmpyinc;
			m_tmpstep --;//round(animalInfo.m_fSpeed);
			if(m_tmpstep<=0){
				m_tmpIsinitPosition = false;
				switch (m_iAction) {
				case EAT:	
					int tmpX = 5 + GameResource.instance.imgFeeder.getWidth() + animalInfo.m_bodyPart[m_iAnimalStatus].m_iW;
					Direction(Direction.RIGHT);
					
					if(m_position.x > tmpX+5 ){
						m_iAction = EAT;
					}
					else{
						m_iAction = STAND ;
						m_iGoToAction = 1;
						m_iGameTick = 100;
					}
					break;
				case TAKEABATH:
					Direction(Direction.LEFT);
					m_iAction = STAND ;
					m_iGoToAction = 2;
					m_bActionBath = true;
					m_iGameTick = 100;
					break;
				default:
					m_iAction =CRes.random(0, 4);
					/*
					if(m_iAction == RUN){
						m_iAction = STAND;
					}
					*/
					isSick();
					
					break;
				}				
			}
		}
		//change frame
		if(animalInfo != null){
			changeFrameRunPet();
		}
		else{
			m_iChangeActionRunDog ++;
			if(m_iChangeActionRunDog > 2){
				m_iActionRunDog = (m_iActionRunDog +1) % 4;
				m_iChangeActionRunDog = 0;
			}
		}
	}
	
	public void updateChat(){
		if(m_bEnableChat){
			m_iTimeShowChat ++;
			if(m_iTimeShowChat > m_iTimeEndChatAnimal){
				m_iTimeShowChat = 0;
				m_bEnableChat = false;
			}
		}
	}
	
	public void update(){
		//update chat
		updateChat();
		
		//update tiền
		if(m_bIsHaveMoney){
			m_iTimeMoney++;
			if(m_iTimeMoney > 2){
				iconIndex++;
				m_iTimeMoney = 0;
			}
		}		
		/////////
				
		switch (m_iAction) {				
		case STAND:
			if(m_iGoToAction != 0){
				m_iGameTick--;
				if(m_iGameTick < 0){
					m_bCompleteAction = true;
					m_iAction = 1;//CRes.random(0, 4);
					m_iGameTick = CRes.random(100, 200);
				}
				EyesTick();
				return;
			}
			break;
		case SLEEP:
			break;
		case SITTING:
			break;
		case RUN:			
		case EAT:
		case TAKEABATH:
			getToPosition();
			break;
		default:
			break;
		}
		
		//////change direction
		if(m_iAction != RUN && m_iAction != EAT && m_iAction != TAKEABATH){
			m_iTimeChangeDirection -- ;
			if(m_iTimeChangeDirection <0){
				Direction(CRes.random(0, 2));
				m_iTimeChangeDirection = CRes.random(50, 100);
			}
			///////change action
			m_iGameTick--;
			if(m_iGameTick < 0){
				m_iAction = RUN;
				isSick();
				m_iGameTick = CRes.random(100, 200);
			}
		}		
		//give it in bound
		if(m_position.x < 0){
			m_position.x = 0;
		}
		if(m_position.x > GameCanvas.w){
			m_position.x = GameCanvas.w;
		}
		if(m_position.y < m_iTop){
			m_position.y = m_iTop;
		}
		if(m_position.y > m_iTop + m_iBoundH){
			m_position.y = m_iTop + m_iBoundH;
		}
		EyesTick();
	}	
	private void EyesTick(){
		//nham mo mat cac con thu
		tickHead--;
		if(animalInfo != null){	
			if(tickHead < 0){
				curHeadFr = 0;//nham mo
				tickHead = CRes.random(20, 30);
			}
			if(curHeadFr == 0 && tickHead < 2){
				curHeadFr = 1;//mo nham
			}
		}
		else{
			if(tickHead < 0){
				m_iEyesDog = 0;//nham mo
				tickHead = CRes.random(20, 30);
			}
			if(curHeadFr == 0 && tickHead < 2){
				m_iEyesDog = 1;//mo nham
			}
		}
	}
	
	public void paintDog(Graphics g){		
		g.drawImage(GameResource.instance.m_arrShadow[1], m_position.x, m_position.y+13, 0);
		switch (m_iAction) {
		case SITTING:
			if(m_position.anchor == Direction.LEFT){				
				GameResource.instance.m_frameBodyDog.drawFrame(3, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+10, m_position.y-11, Sprite.TRANS_NONE, g);
			}
			else{	
				GameResource.instance.m_frameBodyDog.drawFrame(3, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-5, m_position.y-12, Sprite.TRANS_MIRROR, g);
			}
			break;
		case SLEEP:
			if(m_position.anchor == Direction.LEFT){
				GameResource.instance.m_frameBodyDog.drawFrame(4, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+15, m_position.y-6, Sprite.TRANS_NONE, g);
			}
			else{	
				GameResource.instance.m_frameBodyDog.drawFrame(4, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-10, m_position.y-6, Sprite.TRANS_MIRROR, g);
			}
			break;
		case RUN:
			if(m_iActionRunDog == 0 ){// con cho dung
				if(m_position.anchor == Direction.LEFT){
					GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+8, m_position.y-10, Sprite.TRANS_NONE, g);
					
				}
				else{	
					GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-3, m_position.y-11, Sprite.TRANS_MIRROR, g);
				}
			}
			if(m_iActionRunDog == 1){// con cho dam
				if(m_position.anchor == Direction.LEFT){
					GameResource.instance.m_frameBodyDog.drawFrame(1, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+10, m_position.y-10, Sprite.TRANS_NONE, g);
					
				}
				else{	
					GameResource.instance.m_frameBodyDog.drawFrame(1, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-5, m_position.y-10, Sprite.TRANS_MIRROR, g);
				}
			}
			if(m_iActionRunDog ==2){// con cho dung
				if(m_position.anchor == Direction.LEFT){
					GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+8, m_position.y-10, Sprite.TRANS_NONE, g);
				}
				else{	
					GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-3, m_position.y-11, Sprite.TRANS_MIRROR, g);
				}
			}
			if(m_iActionRunDog ==3){// con cho nhay
				if(m_position.anchor == Direction.LEFT){
					GameResource.instance.m_frameBodyDog.drawFrame(2, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+9, m_position.y-13, Sprite.TRANS_NONE, g);
				}
				else{	
					GameResource.instance.m_frameBodyDog.drawFrame(2, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
					GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-5, m_position.y-12, Sprite.TRANS_MIRROR, g);
				}
			}
			break;
		case STAND:
			if(m_position.anchor == Direction.LEFT){
				GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_NONE, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x+8, m_position.y-10, Sprite.TRANS_NONE, g);
				
			}
			else{	
				GameResource.instance.m_frameBodyDog.drawFrame(0, m_position.x, m_position.y, Sprite.TRANS_MIRROR, g);
				GameResource.instance.m_frameHeadDog.drawFrame(m_iEyesDog, m_position.x-3, m_position.y-11, Sprite.TRANS_MIRROR, g);
			}
			
			break;
		default:
			break;
		}
	}
	
	
	private int m_ichangeX;
	private int m_ichangeY;
	
	private void check(){
		m_ichangeX = m_position.x;
		m_ichangeY = m_position.y;
		
		int change;
		change = animalInfo.m_iW[m_iAnimalStatus] - animalInfo.m_headPart[m_iAnimalStatus].m_iW/2;
		m_ichangeY -= animalInfo.m_iH[m_iAnimalStatus];
		
		if(m_position.anchor == Direction.LEFT )
			m_ichangeX += change;
		else
			m_ichangeX -= change;
	}
	
	public void paint(Graphics g){
		
		////////
		paintPet(g);
		
		/////////paint chat
		if(m_bEnableChat){
			paintBoxChatAnimals(g);
		}
		//////////
		check();
		//////
		/////////
		if(m_bIsHaveMoney){
			check();
			GameResource.instance.m_frameCoin.drawFrame(iconIndex % 3, m_ichangeX-3, m_ichangeY-3, Sprite.TRANS_MIRROR, g);
		}
		///
		if(m_bIsFocus){			
			//paint info cua con vat			
			drawBarSelect( g, m_ichangeX, m_ichangeY+5, animalInfo.m_sHealth);
			
		}
		else if(animalInfo != null)
		{			
			m_ichangeX -= 12;
			m_ichangeY -= 24;
			if(m_bIsHungry){
				statusAnimal(g, m_ichangeX, m_ichangeY, 6);				
			}
			else if(m_bIsDirty){
				statusAnimal(g, m_ichangeX, m_ichangeY, 8);	
			}
			else if(m_iSick != -1 && m_iSick != 0 ){
				statusAnimal(g, m_ichangeX, m_ichangeY, 9);	
			}				
		}
		
	}	
	
	private int tickChangeFr = 0;
	private int curBodyFr = 0;
	private int curHeadFr = 0;
	private int tickHead = CRes.random(20, 30);
	
	private void paintPet(Graphics g){
		int trans = ((m_position.anchor == Direction.RIGHT)?0:Sprite.TRANS_MIRROR);
		int frame;
		
		switch(m_iAction){
		case RUN:
		case TAKEABATH:
		case EAT:
			if(m_iAnimalStatus == 0)
				frame = animalInfo.m_arrRunFrame0[curBodyFr];
			else
				frame = animalInfo.m_arrRunFrame1[curBodyFr];
						
			g.drawImage(GameResource.instance.m_arrShadow[m_iAnimalStatus], animalInfo.m_bodyPart[m_iAnimalStatus].getX(frame, m_position.x, trans), m_position.y - GameResource.instance.m_arrShadow[m_iAnimalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[m_iAnimalStatus].paintPart(g, curHeadFr, frame, m_position.x, m_position.y, trans);
			animalInfo.m_bodyPart[m_iAnimalStatus].paintPart(g, frame, frame, m_position.x, m_position.y, trans);
			break;
			
		case SITTING:
			if(animalInfo.m_bodyPart[m_iAnimalStatus].m_nFrame == 5)
				frame = animalInfo.m_bodyPart[m_iAnimalStatus].m_nFrame - 2;
			else
				frame = animalInfo.m_bodyPart[m_iAnimalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[m_iAnimalStatus], animalInfo.m_bodyPart[m_iAnimalStatus].getX(frame, m_position.x, trans), m_position.y - GameResource.instance.m_arrShadow[m_iAnimalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[m_iAnimalStatus].paintPart(g, curHeadFr, frame, m_position.x, m_position.y, trans);
			animalInfo.m_bodyPart[m_iAnimalStatus].paintPart(g, frame, frame, m_position.x, m_position.y, trans);
			break;
			
		case STAND:
			if(m_iGoToAction == 1){
				if(m_iGameTick%5 == 0){
					if(m_iDelta == 0)
						m_iDelta = CRes.random(1, 2);
					else
						m_iDelta = 0;
				}
			}
			g.drawImage(GameResource.instance.m_arrShadow[m_iAnimalStatus], animalInfo.m_bodyPart[m_iAnimalStatus].getX(0, m_position.x, trans), m_position.y - GameResource.instance.m_arrShadow[m_iAnimalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[m_iAnimalStatus].paintPart(g, curHeadFr, 0, m_position.x, m_position.y + m_iDelta, trans);
			animalInfo.m_bodyPart[m_iAnimalStatus].paintPart(g, 0, 0, m_position.x, m_position.y, trans);
			
			break;
		case SLEEP://luon luon lay frame cuoi de ve
			frame = animalInfo.m_bodyPart[m_iAnimalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[m_iAnimalStatus], animalInfo.m_bodyPart[m_iAnimalStatus].getX(frame, m_position.x, trans), m_position.y - GameResource.instance.m_arrShadow[m_iAnimalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[m_iAnimalStatus].paintPart(g, curHeadFr, frame, m_position.x, m_position.y, trans);
			animalInfo.m_bodyPart[m_iAnimalStatus].paintPart(g, frame, frame, m_position.x, m_position.y, trans);
			
			break;
			///////////////////////
		}
	}
	
		
	public void changeFrameRunPet(){
		
		tickChangeFr++;
		if(tickChangeFr > animalInfo.m_iTimeChangeFrame[m_iAnimalStatus]){
			if(m_iAnimalStatus == 0){
				curBodyFr = (curBodyFr + 1) % animalInfo.m_arrRunFrame0.length;
			}
			else{
				curBodyFr = (curBodyFr + 1) % animalInfo.m_arrRunFrame1.length;				
			}
			tickChangeFr = 0;
		}
	}
	
	public void paintAction(Graphics g, int animalStatus, int action, int x, int y, int direction){
		int trans = ((direction == Direction.RIGHT)? 0: Sprite.TRANS_MIRROR);
		int frame;
		
		switch(action){
		case RUN:
		case TAKEABATH:
		case EAT:
			if(animalStatus == 0){
				curBodyFr = curBodyFr % animalInfo.m_arrRunFrame0.length;
				frame = animalInfo.m_arrRunFrame0[curBodyFr];
			}
			else{
				curBodyFr = curBodyFr % animalInfo.m_arrRunFrame1.length;
				frame = animalInfo.m_arrRunFrame1[curBodyFr];
			}
						
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], animalInfo.m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[animalStatus].paintPart(g, curHeadFr, frame, x, y, trans);
			animalInfo.m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			
			
			//update for run
			tickChangeFr++;
			if(tickChangeFr > animalInfo.m_iTimeChangeFrame[animalStatus]){
				if(animalStatus == 0){
					curBodyFr++;
				}
				else{
					curBodyFr++;
				}
				tickChangeFr = 0;
			}
			break;
			
		case SITTING:
			if(animalInfo.m_bodyPart[animalStatus].m_nFrame == 5)
				frame = animalInfo.m_bodyPart[animalStatus].m_nFrame - 2;
			else
				frame = animalInfo.m_bodyPart[animalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], animalInfo.m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[animalStatus].paintPart(g, curHeadFr, frame, x, y, trans);
			animalInfo.m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			break;
			
		case STAND:
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], animalInfo.m_bodyPart[animalStatus].getX(0, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[animalStatus].paintPart(g, curHeadFr, 0, x, y, trans);
			animalInfo.m_bodyPart[animalStatus].paintPart(g, 0, 0, x, y, trans);
			
			break;
		case SLEEP://luon luon lay frame cuoi de ve
			frame = animalInfo.m_bodyPart[animalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], animalInfo.m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			animalInfo.m_headPart[animalStatus].paintPart(g, 1, frame, x, y, trans);
			animalInfo.m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			
			break;
			///////////////////////
		}
		
		if(action != SLEEP)
			EyesTick();
	}
			
	private void statusAnimal(Graphics g, int x, int y, int status){		
		GameResource.instance.m_frameIconItems.drawFrame(status, x, y, Sprite.TRANS_NONE, g);		
	}
	
	private void drawBarSelect(Graphics g, int x, int y, int maxhelthy){
		//g.drawImage(img, x, y, Graphics.HCENTER|Graphics.BOTTOM);
		GameResource.instance.m_frameIconItems.drawFrame(19, x - 12, y - 24, Sprite.TRANS_NONE, g);
		
		int strWidth = BitmapFont.m_bmNormalFont.stringWidth(m_strName);
		int tmpWidth = (strWidth > 43)? strWidth+7 : 50;
		tmpWidth = (tmpWidth > maxhelthy*4+5)? tmpWidth : maxhelthy*4+10;
		drawRect( g, x, y - GameResource.instance.m_frameIconItems.frameHeight, tmpWidth);
		
		int tmpX = maxhelthy*2 -1;
		int tmpY =y-GameResource.instance.m_frameIconItems.frameHeight;
		PaintPopup.paintHealtyBar( g, x - tmpX, tmpY-7, maxhelthy, m_iHealth, m_iBonusHealth);
		BitmapFont.drawNormalFont(g, m_strName, x, tmpY-15, 0x555759, Graphics.HCENTER|Graphics.VCENTER);
	}
	
	private void drawRect(Graphics g, int x, int y, int width){		
		g.setColor(0xffffff);
		g.fillRect(x-width/2, y-23, width, 14);
		
		g.setColor(0x72beed);
		g.fillRect(x-width/2, y-9, width, 9);
		
		g.setColor(0x2b6589);
		g.drawRect(x-width/2, y-23, width, 23);
	}	
	
	public void paintIcon(Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		
		int tmpX = Math.abs(animalInfo.m_headPart[m_iAnimalStatus].m_iW - 32);
		int tmpY = Math.abs(animalInfo.m_headPart[m_iAnimalStatus].m_iH - 32);
		animalInfo.m_headPart[m_iAnimalStatus].paintPart(g, curHeadFr, x + tmpX / 2, y + tmpY / 2, 0);
		EyesTick();	
	}
	
	public void paintInfoAnimal(Graphics g, int x, int y){
		int colorFont = 0x3a001e;
		BitmapFont.drawNormalFont(g, "Sức Khỏe: ", x, y, colorFont, Graphics.LEFT | Graphics.TOP);
		PaintPopup.paintHealtyBar(g, x + BitmapFont.m_bmNormalFont.stringWidth("Sức Khỏe: ") + 5, y + 6, animalInfo.m_sHealth, m_iHealth,  m_iBonusHealth);
		String str = (m_iGender == 0)? "Đực": "Cái";
		BitmapFont.drawNormalFont(g, "Giới Tính: " + str, x, y+15, colorFont, Graphics.LEFT | Graphics.TOP);
		
		BitmapFont.drawNormalFont(g, "Thông minh: " + m_iSmart, x, y+30, colorFont, Graphics.LEFT | Graphics.TOP);
		int timelife = animalInfo.m_sTimeLiving * 480;

		if(m_iLifeTime < timelife)
			str = "Sơ sinh";
		else if(m_iLifeTime > animalInfo.m_sTimeLiving * 1440)
			str = "Già";
		else
			str = "Trưởng thành";
		
		BitmapFont.drawNormalFont(g, "Phát Triển: " + str, x, y + 45, colorFont, Graphics.LEFT | Graphics.TOP);
		if(m_iSick == 0 || m_iSick == -1)
			str = "Bệnh tật: Khỏe mạnh";
		else{
			DrugInfo drugInfo = DataManager.gI().getDrugInfo(m_iSick);
			if(drugInfo != null)
				str = "Bệnh tật: " +drugInfo.m_strName;
			else
				str = "Bệnh tật: ";
		}
		BitmapFont.drawNormalFont(g, str, x, y + 60, colorFont, Graphics.LEFT | Graphics.TOP);
		BitmapFont.drawNormalFont(g, "Thời Gian sống: ", x, y + 75, colorFont, Graphics.LEFT | Graphics.TOP);
		PaintPopup.paintBarBlood(g, x + 80, y + 82, 60, (animalInfo.m_sTimeLiving + m_iBonusTime)*1440, m_iLifeTime, 0x91c6e1, 0x3a001e);
	}
	
	public void paintInRow(Graphics g, int x, int y, int width, int height, int isFocus) {
		// TODO Auto-generated method stub		
		if(isFocus == 0)// mac dinh khi chon
			g.setColor(0xfbd634);
		else if(isFocus == 1)// mac dinh khi ko chon gi het
			g.setColor(0x9be5ff);
		else// mac dinh khi chon con dau tien
			g.setColor(0x9bffa7);
		g.fillRoundRect(x, y, 32, 32, 5, 5);
		g.fillRoundRect(x + 20, y + 3, 130, 26, 10, 10);
		paintIcon(g, x + 2, y + 2);
		int headW = ((animalInfo.m_headPart[m_iAnimalStatus].m_iW+10) > 40)? (animalInfo.m_headPart[m_iAnimalStatus].m_iW+10) : 40 ;
		BitmapFont.drawNormalFont(g, m_strName, x+headW, y+4, 0x3a001e, Graphics.LEFT|Graphics.TOP);
		PaintPopup.paintHealtyBar( g, x+headW, y+20, animalInfo.m_sHealth, m_iHealth, m_iBonusHealth);
		if(m_iGender == 0){
			g.drawRegion(GameResource.instance.imgSex, 0, 0, 12, 12, Sprite.TRANS_NONE, x+130, y+16, Graphics.VCENTER|Graphics.HCENTER);
		}
		else{
			g.drawRegion(GameResource.instance.imgSex, 0, 12, 12, 14, Sprite.TRANS_NONE, x+130, y+16, Graphics.VCENTER|Graphics.HCENTER);
		}
	}
	
	public void paintInfo(Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		int widthBox = 160;
		int heightBox = 85;
		
		boolean flag = false;
		//select the best position for popup
		if(y + heightBox > GameCanvas.h - 20){
			y -= (y + heightBox - (GameCanvas.h - 20));
			x = x + 32;
			flag = true;
		}
		if(x + widthBox > GameCanvas.w){
			x -= (x + widthBox - GameCanvas.w);
			if(flag)
				x -= 32;
		}
				
		//paint bouding
		PaintPopup.paintRoundRect(g, x, y, widthBox, heightBox, 0xffe783);
		
		y += 5;
		BitmapFont.drawBoldFont(g, m_strName, x + widthBox/2, y, 0x3a001e, Graphics.HCENTER | Graphics.HCENTER);
		
		y += 15;
        BitmapFont.drawNormalFont(g, "Giá bán cao nhất: "+animalInfo.m_sMoneyHarvest+" gold", 5 + x, y, 0x3a001e, Graphics.LEFT);
		
		y += 15;
        BitmapFont.drawNormalFont(g, "Sinh tiền: "+animalInfo.m_sMoneyPeriod+" gold/ "+animalInfo.m_sTimeCreateMoney+" phút", 5 + x, y, 0x3a001e, Graphics.LEFT);
        
        y += 15;
        BitmapFont.drawNormalFont(g, "Cấp độ sử dụng: "+animalInfo.m_sLevelRequired, 5 + x, y, 0x3a001e, Graphics.LEFT);
        
        y += 15;
        
        int ngay = ((animalInfo.m_sTimeLiving + m_iBonusTime)*1440 - m_iLifeTime)/1440;
        
        if(ngay > 0){
        	int gio = (((animalInfo.m_sTimeLiving + m_iBonusTime)*1440 - m_iLifeTime)%1440)/60;
        	if(gio > 0)
        		BitmapFont.drawNormalFont(g, "Thời gian còn: "+ngay+" ngày, "+gio+" giờ", 5 + x, y, 0x3a001e, Graphics.LEFT);
        	else
        		BitmapFont.drawNormalFont(g, "Thời gian còn: "+ngay+" ngày, ", 5 + x, y, 0x3a001e, Graphics.LEFT);
        }
        else{
        	BitmapFont.drawNormalFont(g, "Thời gian còn: "+(((animalInfo.m_sTimeLiving + m_iBonusTime)*1440 - m_iLifeTime)%1440)/60+" giờ", 5 + x, y, 0x3a001e, Graphics.LEFT);
        }
	}
	
	public void paintBoxChatAnimals(Graphics g){
		//paint chat box
		int trans = (m_position.anchor == Direction.LEFT)? Sprite.TRANS_MIRROR: Sprite.TRANS_NONE ;
		String[] arrStr = BitmapFont.m_bmFont.splitFontBStrInLine(m_strChatAnimal, 100);
		int yPopup = m_position.y - animalInfo.m_iH[m_iAnimalStatus] - arrStr.length*15;
		int tmpX = (BitmapFont.m_bmFont.stringWidth(m_strChatAnimal) < 100)? BitmapFont.m_bmFont.stringWidth(m_strChatAnimal)+5 : 105;
		int tmpP = m_position.x - (tmpX>>1);
		if(tmpP < 0){
			tmpP = 0;
		}
		if(tmpP+tmpX > GameCanvas.w){
			tmpP = GameCanvas.w - tmpX;
		}
		yPopup -= 10;
		PaintPopup.paintBox(g, tmpP, yPopup, tmpX, arrStr.length * 15, 1, 0xffffff, 0x72beed);	
		for(int i=0 ;i< arrStr.length; i++){
			BitmapFont.drawNormalFont(g, arrStr[i], tmpP + 3, yPopup + i*15, 0x000000, Graphics.LEFT | Graphics.TOP);
		}
		yPopup += 10;
		
		tmpX = (m_position.anchor == Direction.LEFT)?-2:-8;
		g.drawRegion(GameResource.instance.imgChatArrow, 0, 0, 10, 10, trans, m_position.x+tmpX, yPopup+(arrStr.length*15-15)+4,  0);
	}
	public void paintChat(Graphics g){
		if(!m_bEnableChat){
			m_bEnableChat = true;
		}
	}

	public void chat(String string) {
		// TODO Auto-generated method stub
		m_strChatAnimal = string;
		m_bEnableChat = true;
	}
   
}
